package com.casstime.ec.cloud.meeting.web.ftl.template;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

import freemarker.core.Environment;
import freemarker.template.SimpleSequence;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class JSONFtlUtil implements TemplateDirectiveModel{

	@SuppressWarnings("rawtypes")
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		Object object = params.get("object");
		String result = "";
		if(object != null && object instanceof SimpleSequence){
			SimpleSequence simple = (SimpleSequence)object;
			ObjectMapper mapper = new ObjectMapper(); 
			result = mapper.writeValueAsString(simple.toList());
		}
		env.getOut().write(result);
	}
}
